#ifndef TCPSOCKET_H
#define TCPSOCKET_H

#include <QObject>
#include <QTcpSocket>
#include <QHostAddress>

struct AirControlState{
    bool isPowerOn=false;
    QString mode;
    QString speed;
    int temperlature;
};
struct FanState{
    bool isPowerOn=false;
    int level;
};
struct CurtainState{
    int progress;
};

class TcpSocket : public QObject
{
    Q_OBJECT
public:
    explicit TcpSocket(QObject *parent = 0);
    Q_INVOKABLE void connect_To_Host(QString ip,int port);
    Q_PROPERTY(QString tcpState READ tcpState NOTIFY tcpStateChanged)
    Q_PROPERTY(QString conIP READ conIP WRITE setConIP NOTIFY conIPChanged)
    Q_PROPERTY(int queryMode READ queryMode WRITE setQueryMode NOTIFY queryModeChanged)
    QString tcpState();
    QString conIP();
    int queryMode();


    void setConIP(QString ip){
        this->serverIP=ip;
        conIPChanged();
    }
    void setQueryMode(int mode);


    Q_INVOKABLE void sendData(QString data);
    Q_INVOKABLE int outsideLight();
    Q_INVOKABLE int insideTemperlature();
    Q_INVOKABLE int insideWest();

    //air control
    Q_INVOKABLE bool isAirPowerOn();
    Q_INVOKABLE QString currentAirMode();
    Q_INVOKABLE QString currentAirSpeed();
    Q_INVOKABLE int currentAirTemperlature();

    //light
    Q_INVOKABLE int lightStateAtIndex(int i){
        return this->mlightState[i];
    }

    //fans
    Q_INVOKABLE bool isFanPowerOnAtIndex(int i){
        return mfanState[i].isPowerOn;
    }
    Q_INVOKABLE int fanLevelAtIndex(int i){
        return this->mfanState[i].level;
    }

    //curtain
    Q_INVOKABLE int curtainProgressAtIndex(int i){
        return this->mcurtainState[i].progress;
    }

signals:
    void tcpStateChanged();
    void conIPChanged();
    void disconnected();
    void queryModeChanged();

public slots:
    void onStateChanged(QAbstractSocket::SocketState state);
    void readData();
private:
    QTcpSocket *socket;
    QString currentState;
    QString serverIP;
    int mqueryMode;
    int moutsideLight;
    int minsideTemperlature;
    int minsideWest;
    AirControlState mairState;
    int mlightState[6];
    FanState mfanState[4];
    CurtainState mcurtainState[2];


};

#endif // TCPSOCKET_H
